package com.jf.util;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jf.pojo.AlipayBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AliPayUtil {

    @Autowired
    private PayUtil payUtil;


    /**
     26      * 支付接口
     27      *
     28      * @param alipayBean 封装的支付宝入参
     29      * @return 返回支付结果
     30      * @throws AlipayApiException 抛出异常
     31      */
    public String pay(AlipayBean alipayBean) throws AlipayApiException {
        String serverUrl = payUtil.getGatewayUrl();
        String appId = payUtil.getAppId();
        String privateKey = payUtil.getPrivateKey();
        String format = "json";
        String charset = payUtil.getCharset();
        String alipayPublicKey = payUtil.getPublicKey();
        String signType = payUtil.getSignType();
        String returnUrl = payUtil.getReturnUrl();
        String notifyUrl = payUtil.getNotifyUrl();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        return alipayClient.pageExecute(alipayRequest).getBody();
    }

}
